package multicast;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

public class TesteServerSocketMulticast extends Thread {

    private MulticastSocket sock;
    private DatagramPacket dp;
    private byte[] buffer = new byte[28];
    private InetAddress groupAddress;

    public TesteServerSocketMulticast() {
        try {
            groupAddress = InetAddress.getByName("226.1.1.1");
            sock = new MulticastSocket(4413);
            sock.joinGroup(groupAddress);

            System.out.println("Servidor rodando!");
        } catch (IOException e) {
            System.out.println("Erro: N�o pode criar o Socket");
        }
    }

    public void run() {

        while (true) {
            if (sock == null) {
                return;
            }
            try {
                dp = new DatagramPacket(buffer, 28);
                sock.receive(dp);

                System.out.println(new String(buffer));
            } catch (Exception e) {
                System.out.println("Erro: N�o pode se conectar ao cliente!");
            }
        }
    }

    public static void main(String[] args) {
        TesteServerSocketMulticast server = new TesteServerSocketMulticast();
        server.start();
    }
}
